package com.zhyt.intelligentpipeline.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Cracksafety {
    // 韧性比
    private  double K_r;

    //荷载比
    private  double L_r;

    //估曲线的截止线
    private double L_max;

    //裂纹短半袖（裂纹深度），mm
    @NotBlank(message = "裂纹深度必须填写")
    private double CracksShortHalfSleeve;

    //裂纹长半袖（裂纹半长） mm
    @NotBlank(message = "裂纹半长必须填写")
    private double CracksLongHalfSleeve;

    //裂纹中心处与裂纹前缘点的角度
    @NotBlank(message = "裂纹中心处与裂纹前缘点的角度必须填写")
    private double CrackAngle;

    //管道内径 mm
    @NotBlank(message = "管道内径必须填写")
    private double InnerDiameter;

    //管道壁厚  mm
    @NotBlank(message = "管道壁厚必须填写")
    private double PipeWallThickness;

    //裂纹倾斜角度  0°<=t-<=90°
    @NotBlank(message = "裂纹倾斜角度必须填写")
    private double CrackTiltAngle;

    //管道运行压力 MPa
    @NotBlank(message = "管道运行压力必须填写")
    private double PipelineOperationPressure;

    //管材的最小屈服强度
    @NotBlank(message = "管材的最小屈服强度必须填写")
    private  double MinimumYieldStrength;

    //管材最小抗拉强度  MPa
    @NotBlank(message = "管材的最小抗拉强度必须填写")
    private  double MinimumTensileStrength;

    //断裂韧度 mm
    @NotBlank(message = "断裂韧度必须填写")
    private double FractureToughness;

    //弹性模量 Mpa
    @NotBlank(message = "弹性模量必须填写")
    private double ElasticModulus;

    //泊松比，无量纲。（0.3）
    @NotBlank(message = "泊松比必须填写")
    private double PoissonRatio;

    //type 输入类别 0表示输入外表面裂纹，1表示输入内表面裂纹
    @NotBlank(message = "type必须填写")
    private int type;

    /**
     * 返回韧性比K_r
     *
     */
    public static Cracksafety getStressintensityfactor_k_r(Cracksafety cracksafety){
        double radian_O=cracksafety.getCrackAngle()*Math.PI/180;
        double kk=cracksafety.getCracksShortHalfSleeve()/cracksafety.getCracksLongHalfSleeve();
        double E_k=Math.pow((1+1.464*Math.pow(kk,1.65)),0.5);  // 5-20
        double M1=1.13-0.09*kk; //5-22
        double M2=-0.54+0.89/(0.2+kk);
        double M3=0.5-1/(0.65+kk)+14*Math.pow((1-kk),24);
        double g_0=1+(0.1+0.35*Math.pow(kk,2))*(1-Math.sin(radian_O));
        double f_0=Math.pow(Math.pow(Math.sin(radian_O),2)+Math.pow(kk,2)*Math.pow(Math.cos(radian_O),2),0.25);
        double R_0 = cracksafety.getInnerDiameter()+cracksafety.getCrackTiltAngle();
        double h;
        if (cracksafety.type==0)
        {
            h=((R_0*R_0+ cracksafety.getInnerDiameter()* cracksafety.getInnerDiameter())/(R_0*R_0-cracksafety.getInnerDiameter()* cracksafety.getInnerDiameter())-1+0.5*Math.sqrt(cracksafety.getCracksShortHalfSleeve()/cracksafety.getPipeWallThickness()))*cracksafety.getPipeWallThickness()/cracksafety.getInnerDiameter();
        }
        else{
            h=((R_0*R_0+ cracksafety.getInnerDiameter()* cracksafety.getInnerDiameter())/(R_0*R_0-cracksafety.getInnerDiameter()* cracksafety.getInnerDiameter())+1-0.5*Math.sqrt(cracksafety.getCracksShortHalfSleeve()/cracksafety.getPipeWallThickness()))*cracksafety.getPipeWallThickness()/cracksafety.getInnerDiameter();
        }
        double t_=cracksafety.getCrackTiltAngle();
        double w_t_ = 7.616*0.001-2.137*0.001*t_+4.174*0.0001*t_*t_-3.008*0.000001*t_*t_*t_;
        double F1=0.97*(M1+M2*Math.pow(kk,2)+M3*Math.pow(kk,4))*g_0*f_0*h;  //5-21
        double K_I = cracksafety.getPipelineOperationPressure()*cracksafety.getInnerDiameter()*Math.sqrt(Math.PI*cracksafety.getCracksShortHalfSleeve())/cracksafety.getPipeWallThickness()*E_k*F1*w_t_;
        double K_IC=Math.sqrt(1.5*cracksafety.getMinimumYieldStrength()*cracksafety.getFractureToughness()*cracksafety.getElasticModulus()/(1-cracksafety.getPoissonRatio()*cracksafety.getPoissonRatio()));
        double K_r=K_I/K_IC;
        cracksafety.setK_r(K_r);
        return cracksafety;  //韧性比K_r
    }
    /**
     *计算荷载比
     */
    public static Cracksafety get_LoadRatio_L_r(Cracksafety cracksafety){
        double R_0=cracksafety.getInnerDiameter()+cracksafety.getPipeWallThickness();
        double kk = cracksafety.getCracksShortHalfSleeve()/cracksafety.getPipeWallThickness();
        double α = kk/(1+cracksafety.getPipeWallThickness()/cracksafety.getCracksLongHalfSleeve());
        double g=1-20*Math.pow((cracksafety.getCracksShortHalfSleeve()/2*cracksafety.getCracksLongHalfSleeve()),0.75)*Math.pow(α,3);
        double λ_a=1.818*cracksafety.getCracksLongHalfSleeve()/Math.sqrt(cracksafety.getInnerDiameter()*α);
        double M_s=1/(1-kk+kk*λ_a);
        double M_t=Math.sqrt(1+0.4845*λ_a*λ_a);
        double P_m=cracksafety.getPipelineOperationPressure()*cracksafety.getInnerDiameter()/cracksafety.getPipeWallThickness();
        double P_b=cracksafety.getPipelineOperationPressure()*R_0*R_0/(R_0*R_0-cracksafety.getInnerDiameter()*cracksafety.getInnerDiameter())*(cracksafety.getPipeWallThickness()/cracksafety.getInnerDiameter()-3/2*cracksafety.getPipeWallThickness()/cracksafety.getInnerDiameter()*cracksafety.getPipeWallThickness()/cracksafety.getInnerDiameter()+9/5*Math.pow(cracksafety.getPipeWallThickness()/cracksafety.getInnerDiameter(),3));
        double σ_ref=g*P_b+Math.sqrt((Math.pow(g*P_b,2))+9*Math.pow((M_s*P_m*(1-α)*(1-α)),2));
        double L_r=σ_ref/cracksafety.getMinimumYieldStrength();
        cracksafety.setL_r(L_r);
        return cracksafety;
    }

    /**
     *
     * @return  cracksafety.L_max为评估曲线的截止线
     */
    public static Cracksafety get_L_max(Cracksafety cracksafety){
        double L_max=(cracksafety.getMinimumTensileStrength()+cracksafety.getMinimumYieldStrength())/(2*cracksafety.getMinimumYieldStrength());
        cracksafety.setL_max(L_max);
        return cracksafety;
    }

    /**
     *
     * @return 失效评估曲线 f(L_r)
     */
    public static double get_f(Cracksafety cracksafety){
        double f_L_r=(1-0.14*cracksafety.getL_r()*cracksafety.getL_r())*(0.3+0.7*Math.pow(Math.E,-0.65*Math.pow(cracksafety.getL_r(),6)));
        return f_L_r;
    }
}
